<?php
namespace Home\Controller;
use Common\Controller\HomebaseController;
use Home\Service\ClockTimeService;
use Home\Service\AccountService;
// +----------------------------------------------------------------------
// | Date:2017年2月20日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class ScheduleController extends HomebaseController{
    
    protected $type = '';
    public function _initialize(){
       $this->model = D('Schedule');
    }
    
    
    
    /**
     * 列表
     * 
     * date:2017年2月20日
     * author: EK_熊
     */
    public function index(){

        $this->display();
    }
    
    /**
     * 申请记录列表，ajax输出，根据类型type 获取不同的申请数据（附带审核人信息）
     * 
     * date:2017年3月1日
     * author: EK_熊
     */
    public function showlist(){
        $range = I('range');
        $ret = $this->model->showlist($range);
        $weekRange = get_week_range($range);
        $weeks = $weekRange['start'];
        for($i=0;$i<=6;$i++) {
            $data['week'][] = date('Y-m-d',strtotime($weeks)+$i*86400);
        }
        $data['data'] = $ret;
        $data['current_sn'] = $this->user_sn;
        $this->ajaxReturn($data);
    }


    /**
     * 提交申请
     *
     * date:2017年2月20日
     * author: EK_熊
     */
    public function submit(){
        if (IS_POST){
            $post = I('post');
            if (empty($post['date'])) $this->error('请选择日期');
            if (empty($post['text']) && $post['type'] == '外出') $this->error('请填写外出事由');
            if (empty($post['type'])) $this->error('请选择类型');
            $post['sn'] = $this->user_sn;
            $map['sn'] = $this->user_sn;
            $map['date'] = $post['date'];
            $type = $post['type'];
            //先检查是否存在
            $is_exist = $this->model->where($map)->find();
            if ($is_exist) {
                //更新
                $ret = $this->model->where($map)->save($post);
            }else{
                $post['created_time'] = CURRENT_TIME;
                $ret = $this->model->add($post);
            }
            if (!$ret) $this->error('提交失败~~');
            $this->success('提交成功！！');
            exit();
        }
        
            
        $this->display();
    }
    
    
    
    
    private function get_img_path($base64Box){
        if (empty($base64Box)) return '';
        
        for ($i=0;$i<count($base64Box);$i++){
            $path = base64ToImg($base64Box[$i], 'sick');
            if ($path) $ret[] = $path;
        }
        
        return $ret ? json_encode($ret) : false;
    }
    
    
    /**
     * 补办打卡申请，提交页面
     * 
     * date:2017年3月2日
     * author: EK_熊
     */
    public function repair_submit(){
        
        if (IS_POST) {
            $error = '';
            $repairtime = I('repair_time');
            $dutyType = I('duty_type');
            $repa_timestamp = strtotime($repairtime);
            $date = date('Y-m-d',$repa_timestamp);
            if (empty($repairtime)) $this->error('请选择正确的时间');
            if (empty($dutyType)) $this->error('请选择类型');
            
            /* 1.创建申请记录 */
            $attdayModel = D('AttendanceDay');
            
            $attDayInfo = $attdayModel->where(['sn'=>$this->user_sn,'date'=>$date])->field('UNIX_TIMESTAMP(duty_on) as duty_on,UNIX_TIMESTAMP(duty_off) as duty_off')->find();
//             if (empty($attDayInfo)) $this->error("{$date}这天没有您的考勤数据,不能提交此申请！");
            
            $duty_on = (int)$attDayInfo['duty_on'];
            $duty_off = (int)$attDayInfo['duty_off'];
            $this->model->startTrans();
            $add = [
                'sn'=>$this->userInfo['sn'],
                'name'=>$this->userInfo['name'],
                'uid'=>$this->user_uid,
                'total_min'=>0,
                'total_day'=>0,
                'dep_id'=>$this->userInfo['dep_id'],
                'dep_name'=>$this->userInfo['dep_name'],
                'created_time'=>CURRENT_TIME,
                'status'=>0,
                'type'=>4,
                'leader_uid'=>$this->userInfo['leader_id'],
            ];           
            do{
                if ($dutyType == '上班') $add['start_time'] = $repairtime;
                if ($dutyType == '下班') $add['end_time'] = $repairtime;

                $applyid = $this->model->add($add);
                if (!$applyid){
                    $error = '网络异常,操作失败！';
                    break;
                }
                /*添加申请人记录*/
                $appModel = D('Approver'); 
                $ret = $appModel->approver_repair($applyid,$this->userInfo);
                if (!$ret) {
                    $error = $appModel->getError();
                    break;
                }
                
            }while(false);
            
            
            if ($error) {
                $this->model->rollback();
                $this->error($error);
            }else{
                $this->model->commit();
                $this->success('提交成功！',U('index',['type'=>4]));
            }
        }
        $this->display();
    }
    
   
}